package xhwl.hmo.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import xhwl.hmo.mall.R;
import xhwl.hmo.mall.http.bean.resp.OrderListBean;
import xhwl.hmo.mall.ui.AddCommentActivity;
import xhwl.hmo.mall.ui.InfoOrderActivity;
import xhwl.hmo.mall.utils.GlideRoundTransform;
import xhwl.pulltorefresh.adapter.BaseAdapter;
import xhwl.pulltorefresh.adapter.ViewHolder;

import static xhwl.hmo.mall.R.id.order_status;

/**
 * 订单列表的adapter
 */
public class AllOrderAdapter extends BaseAdapter<OrderListBean> implements View.OnClickListener {
    private Context context;
    List<OrderListBean> orderList;
    private Callback callback = null;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.order_cancel:
                break;
            case R.id.pay_start:
                break;
            case R.id.receive_assure:
                break;
        }
        callback.click(v);
    }

    public interface Callback {
        public void click(View v);
    }

    public AllOrderAdapter(Context context, List<OrderListBean> orderList, Callback callback) {
        super(context, R.layout.order_item, orderList);
        this.context = context;
        this.orderList = orderList;
        this.callback = callback;

    }

    @Override
    public void convert(ViewHolder holder, final OrderListBean orderListBean) {
        holder.setText(R.id.num_order, "订单编号: " + orderListBean.getORDER_ID());
        holder.setText(order_status, orderListBean.getORDER_STATUS());
        if (orderListBean.getGOODS_NAME().contains("套装")) {
            String name_goods = orderListBean.getGOODS_NAME();
            String[] strings = name_goods.split("卡");
            holder.setText(R.id.order_name, strings[0] + "卡");
            holder.setText(R.id.order_name_below, strings[1].trim());
        } else {
            holder.setText(R.id.order_name, orderListBean.getGOODS_NAME());
        }
        holder.setText(R.id.price_actual, "￥" + orderListBean.getGOODS_SALE_MONEY() + "/年");
        holder.setText(R.id.price_original, "￥" + orderListBean.getGOODS_MONEY() + "/年");
        TextView order_status = (TextView) holder.getView(R.id.order_status);
        TextView order_cancel = (TextView) holder.getView(R.id.order_cancel);
        TextView pay_start = (TextView) holder.getView(R.id.pay_start);
        TextView receive_assure = (TextView) holder.getView(R.id.receive_assure);
        TextView comment_make = (TextView) holder.getView(R.id.comment_make);
        TextView sum_pay = (TextView) holder.getView(R.id.sum_pay);
        if (orderListBean.getORDER_STATUS().equals("待付款")) {
            String pay_sum = "应付金额: ￥" + orderListBean.getORDER_MONEY();
            SpannableString spannableString = new SpannableString(pay_sum);
            spannableString.setSpan(new AbsoluteSizeSpan(50), 7, pay_sum.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            sum_pay.setText(spannableString);
            order_status.setTextColor(Color.parseColor("#ffa21b"));
            order_cancel.setVisibility(View.VISIBLE);
            pay_start.setVisibility(View.VISIBLE);
            comment_make.setVisibility(View.GONE);
            receive_assure.setVisibility(View.GONE);
        }
        if (orderListBean.getORDER_STATUS().equals("待评价")) {
            String pay_sum = "已付金额: ￥" + orderListBean.getORDER_MONEY();
            SpannableString spannableString = new SpannableString(pay_sum);
            spannableString.setSpan(new AbsoluteSizeSpan(50), 7, pay_sum.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            sum_pay.setText(spannableString);
            order_status.setTextColor(Color.parseColor("#ffa21b"));
            comment_make.setVisibility(View.VISIBLE);
            order_cancel.setVisibility(View.GONE);
            pay_start.setVisibility(View.GONE);
            receive_assure.setVisibility(View.GONE);
        }
        if (orderListBean.getORDER_STATUS().equals("待收货")) {
            String pay_sum = "已付金额: ￥" + orderListBean.getORDER_MONEY();
            SpannableString spannableString = new SpannableString(pay_sum);
            spannableString.setSpan(new AbsoluteSizeSpan(50), 7, pay_sum.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            sum_pay.setText(spannableString);
            order_status.setTextColor(Color.parseColor("#ffa21b"));
            receive_assure.setVisibility(View.VISIBLE);
            order_cancel.setVisibility(View.GONE);
            pay_start.setVisibility(View.GONE);
            comment_make.setVisibility(View.GONE);
        }
        if (orderListBean.getORDER_STATUS().equals("已过期")) {
            String pay_sum = "应付金额: ￥" + orderListBean.getORDER_MONEY();
            SpannableString spannableString = new SpannableString(pay_sum);
            spannableString.setSpan(new AbsoluteSizeSpan(50), 7, pay_sum.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            sum_pay.setText(spannableString);
            order_status.setTextColor(Color.parseColor("#999999"));
            order_cancel.setVisibility(View.VISIBLE);
            pay_start.setVisibility(View.GONE);
            comment_make.setVisibility(View.GONE);
            receive_assure.setVisibility(View.GONE);
        }
        if (orderListBean.getORDER_STATUS().equals("已完成")) {
            String pay_sum = "已付金额: ￥" + orderListBean.getORDER_MONEY();
            SpannableString spannableString = new SpannableString(pay_sum);
            spannableString.setSpan(new AbsoluteSizeSpan(50), 7, pay_sum.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            sum_pay.setText(spannableString);
            order_status.setTextColor(Color.parseColor("#32a7df"));
            pay_start.setVisibility(View.GONE);
            comment_make.setVisibility(View.GONE);
            receive_assure.setVisibility(View.GONE);
            order_cancel.setVisibility(View.GONE);
        }
        if (orderListBean.getORDER_STATUS().equals("待发货")) {
            String pay_sum = "已付金额: ￥" + orderListBean.getORDER_MONEY();
            SpannableString spannableString = new SpannableString(pay_sum);
            spannableString.setSpan(new AbsoluteSizeSpan(50), 7, pay_sum.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            sum_pay.setText(spannableString);
            order_status.setTextColor(Color.parseColor("#ffa21b"));
            pay_start.setVisibility(View.GONE);
            comment_make.setVisibility(View.GONE);
            receive_assure.setVisibility(View.GONE);
            order_cancel.setVisibility(View.GONE);
        }

        final ImageView img_card = (ImageView) holder.getView(R.id.img_card);
        Glide.with(context)
                .load(orderListBean.getGOODS_PIC())
                .transform(new GlideRoundTransform(context))
                .error(R.drawable.logo)
                .into(img_card);
        LinearLayout item_layout = (LinearLayout) holder.getView(R.id.item_layout);
        item_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, InfoOrderActivity.class);
                intent.putExtra(InfoOrderActivity.EXTRA_ORDER_ID, orderListBean.getORDER_ID());
                context.startActivity(intent);
            }
        });
        comment_make.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//订单评价详情
                Intent intent = new Intent(context, AddCommentActivity.class);
                intent.putExtra(AddCommentActivity.EXTRA_BUY_ID, orderListBean.getBUY_ID());
                intent.putExtra(AddCommentActivity.EXTRA_GOOD_ID, orderListBean.getGOODS_ID());
                context.startActivity(intent);
            }
        });
        order_cancel.setOnClickListener(this);
        order_cancel.setTag(orderListBean);
        pay_start.setOnClickListener(this);
        pay_start.setTag(orderListBean);
        receive_assure.setOnClickListener(this);
        receive_assure.setTag(orderListBean);
    }
}
